package com.smarttest.portal.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.smarttest.common.beans.GeneralResultWithStatus;
import com.smarttest.common.util.CommonUtils;
import com.smarttest.common.util.RockectMqProduce;
import com.smarttest.common.util.RocketMQQueryUtils;
import com.smarttest.portal.service.DataProviderService;
@RequestMapping("/dataprovider")
@Controller
public class DataProviderController {
	
	@Autowired
	DataProviderService dataproservice;
	@RequestMapping(value = "/getRocketMQData.action", method = RequestMethod.GET)
	@ResponseBody
	public GeneralResultWithStatus getRocketMQData(HttpServletRequest request) {
		String namesrvAddr = request.getParameter("nameserver");
		String topic = request.getParameter("topic");
		String key = request.getParameter("imei");
		List<Map<String, Object>> datas = new ArrayList<Map<String,Object>>();
		try{
			datas = getMQDatas(namesrvAddr, topic, key);
		}
		catch (Exception e) {
			e.printStackTrace();
			return GeneralResultWithStatus.error("查询MQ异常");
		}
		return GeneralResultWithStatus.okData(null, datas);
	}
	
	private List<Map<String, Object>> getMQDatas(String namesrvAddr, String topic, String key)  throws Exception{
		List<MessageExt> l = RocketMQQueryUtils.QueryMessageExtByKeySubCommand(namesrvAddr, topic, key, 64, 0,
				Long.MAX_VALUE);
		List<Map<String,Object>> ls = new ArrayList<Map<String,Object>>();
		//获取到MQ消息后，同时获取每条消息的入库时间、offset
		for (int i = l.size() - 1; 0 <= i && i < l.size(); i--) {
			MessageExt msg = l.get(i);
			long time = msg.getStoreTimestamp();
			long offset = msg.getQueueOffset();
//			JSONObject jo = JSONObject.parseObject(new String(msg.getBody()));
			Map<String,Object> m = new HashMap<String, Object>();
			m.put("mqstoragetime", time);
			m.put("offset", offset);
			m.put("message", new String(msg.getBody()));
			ls.add(m);
		}
		return ls;
	}
	@RequestMapping(value = "/importRocketMQData.action",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public  GeneralResultWithStatus importRocketMQData(HttpServletRequest request) throws ParseException {
		String filename = request.getParameter("filename");
		String sheetname = request.getParameter("sheetname");
		String nameserver = request.getParameter("nameserver");
		String topic = request.getParameter("topic");
		String imei = request.getParameter("imei");
		String tag = request.getParameter("tag");
		String sendtime = request.getParameter("starttime");
		String timeformat = request.getParameter("timeformat");
		if (sendtime.isEmpty()) {
			sendtime = CommonUtils.getNow(timeformat);
		}
//		timeformat = timeformat.replaceAll("m", "M").replaceAll("i", "m");
//		if (timeformat.contains("h")) {
//			timeformat = timeformat.replaceAll("h", "H");
//		}
//		else if (timeformat.contains("H")) {
//			timeformat = timeformat.replaceAll("H", "h");
//		}
		int timeInterval = 0;
		if (request.getParameter("timeInternal").isEmpty()) {
			timeInterval = 0;
		}
		else {
			timeInterval = Integer.parseInt(request.getParameter("timeInternal"));
		}
		int sendInternal = 0;
		if (request.getParameter("sendInternal").isEmpty()) {
			sendInternal = 0;
		}
		else {
			sendInternal = Integer.parseInt(request.getParameter("sendInternal"));
		}
		List<Map<String, Object>> datas = new ArrayList<Map<String,Object>>();
		try {
			//每次生产新建一个生产者组
			RockectMqProduce rmqproduce = new RockectMqProduce(topic+"_"+getRandom()+"_"+CommonUtils.getNow("yyyyMMdd_HHmmss"), nameserver, "instance");
			rmqproduce.start();
			if (sheetname.isEmpty()) {
				rmqproduce.sendMsgFromFile(filename, topic, tag, imei, sendtime, timeformat, timeInterval,sendInternal);
			}
			else{
				rmqproduce.sendMsgForExcel(filename, sheetname, topic, tag, imei, sendtime, timeformat, -1, timeInterval,sendInternal);
			}
			rmqproduce.close();
			datas = getMQDatas(nameserver, topic, imei);
		}
		catch (Exception e) {
			e.printStackTrace();
			return GeneralResultWithStatus.error("导入失败");
		}
		return GeneralResultWithStatus.okData(null, datas);
	}
	@RequestMapping(value = "/getHbases.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus getHbases(HttpServletRequest request) {
		return GeneralResultWithStatus.okData(null, dataproservice.getHbases());
	}
	@RequestMapping(value = "/getHbaseTables.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus getHbaseTables(HttpServletRequest request) {
		int hbaseid = Integer.parseInt(request.getParameter("hbaseid"));
		return GeneralResultWithStatus.okData(null, dataproservice.getHbaseTables(hbaseid));
	}
	@RequestMapping(value = "/getHbasekeys.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus getHbasekeys(HttpServletRequest request) {
		if (null == request.getParameter("tbid")) {
			return GeneralResultWithStatus.okData(null, null);
		}
		else {
			int tbid = Integer.parseInt(request.getParameter("tbid"));
			return GeneralResultWithStatus.okData(null, dataproservice.getHbasekeys(tbid));
		}
	}
	@RequestMapping(value = "/getHbasePro.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus getHbasePro(HttpServletRequest request) {
		int hbaseid = Integer.parseInt(request.getParameter("hbaseid"));
		return GeneralResultWithStatus.okData(null, dataproservice.getHbasePro(hbaseid));
	}
	
	@RequestMapping(value = "/updateRowkeyrule.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus updateRowkeyrule(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		String rowkeyrule = request.getParameter("rowkeyrule");
		return GeneralResultWithStatus.okData(null, dataproservice.updateRowkey(tid, rowkeyrule));
	}
	@RequestMapping(value = "/updatehbasepro.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus updatehbasepro(HttpServletRequest request) {
		int hid = Integer.parseInt(request.getParameter("hid"));
		String rootdir = request.getParameter("rootdir");
		String znodeparent = request.getParameter("znodeparent");
		String quorum = request.getParameter("quorum");
		String usehostnameonly = request.getParameter("usehostnameonly");
		return GeneralResultWithStatus.okData(null, dataproservice.updateHbasePro(hid, rootdir, znodeparent, quorum, usehostnameonly));
	}
	@RequestMapping(value = "/addHbase.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus addHbase(HttpServletRequest request) {
		String name = request.getParameter("name");
		String rootdir = request.getParameter("rootdir");
		String znodeparent = request.getParameter("znodeparent");
		String quorum = request.getParameter("quorum");
		String usehostnameonly = request.getParameter("usehostnameonly");
		return GeneralResultWithStatus.okData(null, dataproservice.addHbase(name, rootdir, znodeparent, quorum, usehostnameonly));
	}
	@RequestMapping(value = "/addHTable.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus addHTable(HttpServletRequest request) {
		String name = request.getParameter("name");
		int hid = Integer.parseInt(request.getParameter("hid"));
		int usemd5 = Integer.parseInt(request.getParameter("usemd5"));
		return GeneralResultWithStatus.okData(null, dataproservice.addHtable(hid,name,usemd5));
	}
	@RequestMapping(value = "/addRowkeyRule.action", method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public GeneralResultWithStatus addRowkeyRule(HttpServletRequest request) {
		int tid = Integer.parseInt(request.getParameter("tid"));
		String rowkeyrule = request.getParameter("rowkeyrule");
		return GeneralResultWithStatus.okData(null, dataproservice.addRowkeyRule(tid, rowkeyrule));
	}
	@RequestMapping(value = "/getRowkeySearchStr.action",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public  GeneralResultWithStatus getRowkeySearchStr(HttpServletRequest request) {
		int tbid = Integer.parseInt(request.getParameter("tbid"));
		return GeneralResultWithStatus.okData(null, dataproservice.getRowkeySearchStr(tbid));
	}
	@RequestMapping(value = "/getHbaseResult.action",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public  GeneralResultWithStatus getHbasekeyRule(HttpServletRequest request) {
		JSONObject jo = JSON.parseObject(request.getParameter("searchstr"));
		int tid = Integer.parseInt(request.getParameter("tid"));
		int hid = Integer.parseInt(request.getParameter("hid"));
		return GeneralResultWithStatus.okData(null, dataproservice.getHbaseResult(hid,tid,jo));
	}
	@RequestMapping(value = "/getHbaseResultNew.action",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public  GeneralResultWithStatus getHbaseResultNew(String searchstr, Integer tid,Integer hid) {
		return GeneralResultWithStatus.okData(null, dataproservice.getHbaseResult_New(hid,tid,JSON.parseObject(searchstr)));
	}
	@RequestMapping(value = "/getHbaseNames.action",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public  GeneralResultWithStatus getHbaseNames(HttpServletRequest request) {
		return GeneralResultWithStatus.okData(null, dataproservice.getHbaseNames());
	}
	@RequestMapping(value = "/getHtableNames.action",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public  GeneralResultWithStatus getHtableNames(HttpServletRequest request) {
		int hid = Integer.parseInt(request.getParameter("hid"));
		return GeneralResultWithStatus.okData(null, dataproservice.getHtableNames(hid));
	}
	@RequestMapping(value = "/getMongodbData.action",method = RequestMethod.GET)
	@ResponseBody
	public  GeneralResultWithStatus getMongodbData(HttpServletRequest request) {
		GeneralResultWithStatus result;
		return null;
	}
	/**
	 * 获得随机数
	 * @return
	 */
	public int getRandom() {
		Random random = new Random();
		int x = random.nextInt(899999);

		x = x+100000;
		return x;
	}
}
